package com.jtjrenren.android.taxi.driver.base;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/1/13.
 */
public abstract class BaseRcyAdapter<T> extends RecyclerView.Adapter {
    public final static int MODE_NORMAL = 0x1; //没有头部 没有底部
    public final static int MODE_HEADER = 0x2;//只有头部
    public final static int MODE_FOOTER = 0x3;//只有底部
    public final static int MODE_BOTH = 0x4;//头部底部都有
    public final static int TYPE_ITEM = 0x5;
    public final static int TYPE_HEADER = 0x6;
    public final static int TYPE_FOOTER = 0x7;


    protected LayoutInflater inflater;
    protected List<T> list;
    protected int mode = MODE_NORMAL;

    protected Context context;

    public BaseRcyAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        list = new ArrayList<>();
    }

    public void appendList(List<T> appendList) {
        int start = list.size() > 0 ? list.size() : 0;
        if (list != null && list.size() >= 0) {
            list.addAll(appendList);
        }
        notifyItemRangeInserted(start + 1, appendList.size());
    }

    public void add(T t) {
        int start = list.size() > 0 ? list.size() : 0;
        if (list != null && list.size() >= 0) {
            list.add(t);
        }
        notifyItemRangeInserted(start + 1, 1);
    }

    public void replaceList(List<T> replaceList) {
        if (list != null)
            list.clear();
        list.addAll(replaceList);
        notifyDataSetChanged();
    }

    public void replace(List<T> rList) {
        list.clear();
        if (rList != null)
            list.addAll(rList);

        notifyDataSetChanged();
    }

    public abstract RecyclerView.ViewHolder getItemViewHolder(ViewGroup parent, int viewType);

    public abstract void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position);

    /**
     * 用户如果想要自定义item的item样式
     *
     * @param position
     * @return
     */
    public int getItemViewTypeItem(int position) {
        return TYPE_ITEM;
    }

    public RecyclerView.ViewHolder getHeaderViewHolder(ViewGroup parent) {
        return null;
    }

    public RecyclerView.ViewHolder getFooterViewHolder(ViewGroup parent) {
        return null;
    }

    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) {
    }

    public void onBindFooterViewHolder(RecyclerView.ViewHolder holder) {
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        if (viewType == TYPE_FOOTER)
            holder = getFooterViewHolder(parent);
        else if (viewType == TYPE_HEADER)
            holder = getHeaderViewHolder(parent);
        else
            holder = getItemViewHolder(parent, viewType);

        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (mode == MODE_HEADER) {
            if (position == 0) {
                onBindHeaderViewHolder(holder);
                return;
            }
        } else if (mode == MODE_FOOTER) {
            if (position == getItemCount() - 1) {
                onBindFooterViewHolder(holder);
                return;
            }
        } else if (mode == MODE_BOTH) {
            if (position == 0) {
                onBindHeaderViewHolder(holder);
                return;
            } else if (position == getItemCount() - 1) {
                onBindFooterViewHolder(holder);
                return;
            }
        }
        if (mode == MODE_HEADER || mode == MODE_BOTH)
            position--;
        onBindItemViewHolder(holder, position);
    }

    @Override
    public int getItemCount() {
        int count = list.size();
        if (mode == MODE_FOOTER || mode == MODE_HEADER)
            count += 1;
        else if (mode == MODE_BOTH)
            count += 2;
        return count;
    }

    /**
     * 获取item的实际位置
     *
     * @param position
     * @return
     */
    public int getItemPosition(int position) {
        if (mode == MODE_BOTH || mode == MODE_HEADER)
            return position - 1;
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        if (mode == MODE_HEADER) {
            if (position == 0)
                return TYPE_HEADER;
        } else if (mode == MODE_FOOTER) {
            if (position == getItemCount() - 1)
                return TYPE_FOOTER;
        } else if (mode == MODE_BOTH) {
            if (position == 0)
                return TYPE_HEADER;
            else if (position == getItemCount() - 1)
                return TYPE_FOOTER;
        }
        if (mode == MODE_HEADER || mode == MODE_BOTH)
            position--;
        return getItemViewTypeItem(position);
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
        notifyDataSetChanged();
    }


}
